import React, {Component} from "react";
import ReactEcharts from 'echarts-for-react';
import picture from '../timg.jpg';
import axios from "axios";
import {server_addr} from "../index";

export default class Debt extends Component {
    constructor(props) {
        super(props);
        this.state = {
            option_1: {},
            option_2: {},
            equityRatio: 0,
            fixedAssetsRaito: 0,
            longTermDebtRatio: 0,
            cashRatio: 0,
            default: "83%"
        }
        this.getData = this.getData.bind(this);
        this.Format = this.Format.bind(this);
    }

    getData(){
        var stock = window.location.href.split('?')[1].split('=')[1];
        //资产负债率
        axios.get(server_addr+"/debt/assetsDebtRatio?stock="+stock,{}).then((res)=>{
            let dates = [];
            let data_1 = [];
            let data_2 = [];
            for(var key in res.data){
                dates.push(key);
                data_1.push(res.data[key].split(',')[0]);
                data_2.push(res.data[key].split(',')[1]);
            }
            this.setState({
                option_1:{
                    title: {left: 100, text: '平均资产负债率'},
                    legend: {top: 20, left: 250, data: ['平均资产负债率', '行业平均资产负债率']},
                    tooltip: {trigger: 'axis', transitionDuration:0,
                        formatter: function (params) {
                            var htmlStr = '';
                            htmlStr += '<div><span style="color:#fff;">' + params[0].name + '</span><br/> ';
                            for(var i=0;i<params.length;i++){
                                //前面的原点和他的颜色
                                htmlStr += '<span style="width: 8px;height: 8px;display:inline-block;border-radius: 50%;background-color:'+params[i].color+'"></span><span style="color:#fff;">&nbsp;' + params[i].seriesName + '&nbsp;:&nbsp;</span>'+
                                    '<span style="color:#fff;">' + params[i].value + '&nbsp;%</span><br/>';
                            }
                            htmlStr += '</div>';
                            return  htmlStr
                        }},
                    grid: {
                        left: "20%",
                        right: "2%"
                    },
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: dates
                        }
                    ],
                    yAxis: [{type: 'value',min: 'dataMin',max: 'dataMax',name: '%        '}],
                    series: [
                        {
                            name: '平均资产负债率',
                            type: 'line',
                            smooth: true,
                            stack: '总量',
                            data: data_1
                        },
                        {
                            name: '行业平均资产负债率',
                            type: 'line',
                            smooth: true,
                            stack: '总量',
                            itemStyle:{
                                normal:{
                                    color:'green',
                                }
                            },
                            data: data_2
                        }
                    ]
                }
            })
        }).catch(function (error) {
            alert('未连接到服务器！');
        })
        //流动比率
        axios.get(server_addr+"/debt/flowRatio?stock="+stock,{}).then((res)=>{
            let dates = [];
            let data_1 = [];
            let data_2 = [];
            let data_3 = [];
            for(var key in res.data){
                dates.push(key);
                data_1.push(res.data[key].split(',')[0]);
                data_2.push(res.data[key].split(',')[1]);
                data_3.push(res.data[key].split(',')[2]);
            }
            this.setState({
                option_2:{
                    title: {left: 100, text: '流动比率/速动比率'},
                    legend: {top: 20, left: 550, data: ['流动比率', '行业平均流动比率', '速动比率']},
                    tooltip: {
                        trigger: 'axis',
                        transitionDuration:0,
                        axisPointer: {
                            type: 'cross',
                            crossStyle: {
                                color: '#999'
                            }
                        }},
                    grid: {
                        left: "12%",
                        right: "3%"
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: dates,
                            axisPointer: {
                                type: 'shadow'
                            }
                        }
                    ],
                    yAxis: [{type: 'value'}],
                    series: [
                        {
                            name: '流动比率',
                            type: 'bar',
                            data: data_1,
                            itemStyle:{
                                normal:{
                                    barBorderRadius: 3
                                }
                            }
                        },
                        // {
                        //     name: '行业平均流动比率',
                        //     type: 'bar',
                        //     itemStyle:{
                        //         normal:{
                        //             color:'green',
                        //         }
                        //     },
                        //     data: data_2
                        // },
                        {
                            name: '速动比率',
                            type: 'bar',
                            itemStyle:{
                                normal:{
                                    color:'green',
                                    barBorderRadius: 3
                                }
                            },
                            data: data_3
                        }
                    ]
                }
            })
        }).catch(function (error) {
            alert('未连接到服务器！');
        })
        //产权比率
        axios.get(server_addr+"/debt/equityRatio?stock="+stock,{}).then((res)=>{
            this.setState({
                equityRatio: res.data
            })
        }).catch(function (error) {
            alert('未连接到服务器！');
        })
        //资本固定化比率
        axios.get(server_addr+"/debt/fixedAssetsRaito?stock="+stock,{}).then((res)=>{
            this.setState({
                fixedAssetsRaito: res.data
            })
        }).catch(function (error) {
            alert('未连接到服务器！');
        })
        //长期负债比率
        axios.get(server_addr+"/debt/longTermDebtRatio?stock="+stock,{}).then((res)=>{
            this.setState({
                longTermDebtRatio: res.data
            })
        }).catch(function (error) {
            alert('未连接到服务器！');
        })
        //现金比率
        axios.get(server_addr+"/debt/cashRatio?stock="+stock,{}).then((res)=>{
            this.setState({
                cashRatio: res.data
            })
        }).catch(function (error) {
            alert('未连接到服务器！');
        })
    }

    Format(dates){
        let new_dates = []
        for(var i=0; i<dates.length; i++){
            switch (dates[i].split('-')[1]) {
                case '03':
                    new_dates.push(dates[i].split('-')[0].substring(2,)+'年季1');
                    break;
                case '06':
                    new_dates.push(dates[i].split('-')[0].substring(2,)+'年季2');
                    break;
                case '09':
                    new_dates.push(dates[i].split('-')[0].substring(2,)+'年季3');
                    break;
                case '12':
                    new_dates.push(dates[i].split('-')[0].substring(2,)+'年季4');
                    break;
                default:
                    new_dates.push(dates[i]);
            }
        }
        return new_dates;
    }

    componentDidMount() {
        this.getData()
        this.interval = setInterval(()=>{
            if(this.props.location.state){
                this.setState({
                    default: "94%"
                })
            }else{
                this.setState({
                    default: "83%"
                })
            }
        },100)
    }

    componentWillUnmount() {
        clearInterval(this.interval)
    }

    render() {
        document.title = "财务状况分析";
        return (
            <div style={{width:this.state.default, height:"100vh", float:"right"}}>
                <div style={{width:"55%", height:"45vh", float:"left", margin:"5vh 0 0 0"}}>
                    <ReactEcharts option={this.state.option_1} notMerge={true}
                                  style={{width:"100%", height:"45vh"}}/>
                </div>
                <div style={{width:"38%", height:"45vh", float:"left", margin:"2vh 0 0 2%"}}>
                    <div style={{width:"40%", height:"20vh", float:"left", margin:"2vh 0 0 0"}}>
                        <p style={{fontWeight:"bold", float:"left", fontSize:"1.5vw", margin:"2vh 0 0 15%"}}>产权比率&nbsp;</p>
                        <p style={{fontSize:"3vw", fontWeight:"900", marginLeft:"14%", color:"lightseagreen"}}>{this.state.equityRatio} </p>
                    </div>
                    <div style={{width:"40%", height:"20vh", float:"left", fontSize:"1.5vw", margin:"2vh 0 0 5%"}}>
                        <p style={{fontWeight:"bold", float:"left", margin:"2vh 0 0 15%"}}>资产固定化比率</p>
                        <p style={{fontSize:"3vw", fontWeight:"900", marginLeft:"14%", color:"lightseagreen"}}>{this.state.fixedAssetsRaito} </p>
                    </div>
                    <div style={{width:"40%", height:"20vh", float:"left", fontSize:"1.5vw", margin:"2vh 0 0 0"}}>
                        <p style={{fontWeight:"bold", float:"left", margin:"2vh 0 0 14%"}}>长期负债&nbsp;</p>
                        <p style={{fontSize:"3vw", fontWeight:"900", marginLeft:"15%", color:"lightseagreen"}}>{this.state.longTermDebtRatio} </p>
                    </div>
                    <div style={{width:"40%", height:"20vh", float:"left", fontSize:"1.5vw", margin:"2vh 0 0 5%"}}>
                        <p style={{fontWeight:"bold", float:"left", margin:"2vh 0 0 14%"}}>现金比率&nbsp;</p>
                        <p style={{fontSize:"3vw", fontWeight:"900", marginLeft:"15%", color:"lightseagreen"}}>{this.state.cashRatio} </p>

                    </div>
                </div>
                <div style={{width:"95%", height:"48vh", float:"left"}}>
                    <ReactEcharts option={this.state.option_2} notMerge={true}
                                  style={{width:"95%", height:"48vh"}}/>
                </div>
            </div>
        );
    }
}